﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using CTRL;
using BLL;
using DAL;

namespace GUI_Client
{
    public partial class GUICars : Form
    {
        public static Customer Customer { get; private set; }
        private CtrlCar ctrlCar;
        private static GUICars instance;

        private GUICars()
        {
            InitializeComponent();
            ctrlCar = CtrlCar.Instance;
            Customer = GUIMain.Customer;
            UpdateTable();
        }

        public static GUICars Instance
        {
            get
            {
                if (instance == null)
                    instance = new GUICars();

                return instance;
            }
        }

        private void UpdateTable()
        {
            dataGridView1.Rows.Clear();
            List<Car> list = ctrlCar.GetAllCarsOwnedByCustomer(Customer.customerID).ToList();
            foreach (Car car in list)
                dataGridView1.Rows.Add(car.carID, car.range, car.capacity, car.licencePlate);
        }


        private void addC_Click(object sender, EventArgs e)
        {
            ctrlCar.CreateCar(double.Parse(dataGridView1.CurrentRow.Cells[1].Value.ToString()),
                double.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString()),
                Customer.customerID,
                dataGridView1.CurrentRow.Cells[3].Value.ToString());
            UpdateTable();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ctrlCar.UpdateCar(int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()),
                double.Parse(dataGridView1.CurrentRow.Cells[1].Value.ToString()),
                double.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString()),
                Customer.customerID,
                dataGridView1.CurrentRow.Cells[3].Value.ToString());
            UpdateTable();
        }

        private void deleteC_Click(object sender, EventArgs e)
        {
            ctrlCar.DeleteCar(int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString()));
            UpdateTable();
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIMain main = GUIMain.Instance;
            main.Show();

            this.Hide();
        }
    }
}